//
//  ViewController.m
//  HDLianXuDingWeiDemo
//
//  Created by HDHaoShaoPeng on 2017/5/6.
//  Copyright © 2017年 HDHaoShaoPeng. All rights reserved.
//

#import "ViewController.h"
#import "HD_NKM_Mediator.h"
#import "HD_HSP_Location.h"
#import "HDDeclare.h"
#import "HDMapView.h"
#import "UIView+Extension.h"
#import "SSZipArchive.h"
#import "HDDeclare.h"
#import "HDSettingViewController.h"

@interface ViewController ()<HDMapViewDataSource,HDMapViewDelegate,HD_HSP_Location_Delegate,HDSettingViewControllerDelegate>
@property(strong,nonatomic)HD_NKM_Mediator *iBeaconManager;
@property(strong,nonatomic)HD_HSP_Location *locationManager;
@property(strong,nonatomic)HDMapView *myMapView;

@property(nonatomic,weak)HDDeclare *declare;

@property (weak, nonatomic) IBOutlet UITextView *iBeaconTextView;

@end

@implementation ViewController
{
    NSString *_machineString;
    NSString *_infoMessage;
    HDSettingViewController *_settingVC;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    [self unZipMapResource];
    
    [self.view insertSubview:self.myMapView
                     atIndex:0];//添加瓦片地图
    [self addAnnatation];
    
    [self.iBeaconManager startRangingAllRegions];//开始接收蓝牙信号。
    
    [self initLocationManagerConfiguration];//初始化定位模块
    
    [[NSNotificationCenter defaultCenter] addObserver:self.locationManager
                                             selector:@selector(udpParameterWith:)
                                                 name:kSuccessive_Location_Beacon_Notification
                                               object:nil];
}


-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"showSetting"]) {
        HDSettingViewController *settingVC = segue.destinationViewController;
//        [settingVC showMachineStringWith:_machineString];
//        [settingVC showMessagesWith:_infoMessage];
        settingVC.myDelegate = self;
        _settingVC = settingVC;
        settingVC.machineString = _machineString;
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - HD_HSP_Location_Delegate

-(void)feedbackiBeaconMessagesWith:(NSString *)string
{
    dispatch_async(dispatch_get_main_queue(), ^{
        self.iBeaconTextView.text = string;
    });
}

-(void)updateInfoMessagesWith:(NSString *)infoMessage
{
    if (_settingVC) {
        [_settingVC showMessagesWith:infoMessage];
    }
}

-(void)updatePositionintWith:(CGPoint)point
{
    CGPoint iosPoint = CGPointMake(point.x*250/8.0, point.y*250/8.0);
    [self updateAnnotationWith:iosPoint];
}

-(void)pleaseInputIdentifier
{
//    self showAlertWithMessage:@"请输入"
}

#pragma mark - 点击方法

- (IBAction)settingClicked:(id)sender {
    [self performSegueWithIdentifier:@"showSetting" sender:nil];
}





#pragma mark - 私有方法

-(void)showAlertWithMessage:(NSString *)message
{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:message preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:nil];
    [alert addAction:action];
    
    
    [self presentViewController:alert animated:YES completion:nil];
    
}

-(void)initLocationManagerConfiguration
{
    if (self.locationManager) {}
}

-(void)unZipMapResource
{
    if (![[NSFileManager defaultManager]fileExistsAtPath:self.declare.catch_path]) {
        [[NSFileManager defaultManager] createDirectoryAtPath:self.declare.catch_path withIntermediateDirectories:NO attributes:nil error:nil];
    }
    
    NSString *filePath = [[NSBundle mainBundle]pathForResource:@"tileView" ofType:@"zip"];
    NSString *destinationPath = self.declare.catch_path;
    if (![[NSFileManager defaultManager]fileExistsAtPath:[self.declare.catch_path stringByAppendingPathComponent:@"tileView"]]) {
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            if ([SSZipArchive unzipFileAtPath:filePath toDestination:destinationPath]) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    [self.myMapView layoutIfNeeded];
                });
            }
        });
    }
    else
    {
        [self.myMapView layoutIfNeeded];
    }
}

-(void)addAnnatation
{
    
    float x = 125.0;
    float y = 125.0;
    
    
    HDAnnotation *ann = [HDAnnotation annotationWithPoint:CGPointMake(x, y)];
    [self.myMapView addAnnotation:ann animated:YES];
    
    
    [self.myMapView setZoomScale:2.0 animated:YES];
}

-(void)updateAnnotationWith:(CGPoint)point
{
    [self.myMapView updatePinPositionWith:point];
}

#pragma mark - 懒加载

-(HDDeclare *)declare
{
    if (!_declare) {
        _declare = [HDDeclare shareDeclare];
    }
    return _declare;
}

-(HDMapView *)myMapView
{
    if (!_myMapView) {
        CGSize mapSize = CGSizeMake(250, 250);
        _myMapView = [[HDMapView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, self.view.height - 0) contentSize:mapSize];
        _myMapView.dataSource = self;
        _myMapView.mapViewdelegate = self;
        _myMapView.levelsOfZoom = 2;
        _myMapView.levelsOfDetail = 2;
    }
    return _myMapView;
}

-(HD_NKM_Mediator *)iBeaconManager
{
    if (!_iBeaconManager) {
        _iBeaconManager = [HD_NKM_Mediator shareInstance];
    }
    return _iBeaconManager;
}

-(HD_HSP_Location *)locationManager
{
    if (!_locationManager) {
        _locationManager = [[HD_HSP_Location alloc]initWithServerIP:@"192.168.11.14" andServerAdress:@"http://192.168.11.14:8002/ServiceApi.asmx" andSoapActionAdress:@"http://www.uradiosys.com/GetTagStatus" anduploadPort:10615];
        _locationManager.myDelegate = self;
        
        _machineString = @"90:FD:61:85:4A:23";
        _locationManager.macAdress = _machineString;
    }
    return _locationManager;
}



#pragma mark - HDSettingViewControllerDelegate

-(void)machineStringBackWith:(NSString*)string
{
    _machineString = string;
    self.locationManager.macAdress = _machineString;
}



#pragma mark - mapViewdelegate

- (void)mapViewDidZoom:(HDMapView *)mapView
{
}

- (void)mapViewDidScroll:(HDMapView *)mapView
{
}

- (void)mapView:(HDMapView *)mapView didReceiveSingleTap:(UIGestureRecognizer *)gestureRecognizer
{
}



#pragma mark - mapViewSource delegate

- (UIImage *)mapView:(HDMapView *)mapView
         imageForRow:(NSInteger)row
              column:(NSInteger)column
               scale:(NSInteger)scale
{
    NSString *folderName = @"125";
    if (scale == 2)
        folderName = @"250";
    else if (scale == 4)
        folderName = @"500";
    else if (scale == 8)
        folderName = @"1000";
    
    NSString *name = [NSString stringWithFormat:@"%@/tileView/%@/1_%@_%ld_%ld.png",self.declare.catch_path,folderName, folderName,(long)column, (long)row];
    UIImage *myImage = [UIImage imageWithContentsOfFile:name];
    
    return myImage;
}

@end















